#!/bin/csh -f 
#
#
# -c do not cd to home directory
# -n do -not rewind, extract next file
# -f specify filename for tar command
#

# make sure path contains necessary directories
set path = (/usr/bsd /usr/ucb /bin /usr/bin /etc /usr/etc $path)
rehash 

# check if user is root
set id_line = `id | fgrep 'root'`
if (! $status) then
  echo do not run this script as root
  exit 1
endif

set do_not_rewind=0
set do_cd_home=1
set position = 2    # default position is after second tapemarker

# which machine is this script running on
set MACHINE = `uname -m`
switch ($MACHINE)
  case IP[0-9]:
  case IP[0-9][0-9]:
  case IP[0-9][0-9][0-9]:
    set machine = 'iris'
    breaksw;
  case 'sun':
  case 'sun4':
  case 'sun4[a-z0-9]':
    set machine = 'sun4'
    breaksw;
  case 9000/7[0-9][0-9]:
    set machine = 'hp'
    breaksw;
  case 'MIPS':
  case 'RISC':
    set machine = 'dec'
    breaksw;
  default:
    set probl_no = 2
    goto problem
endsw

set nr_tape = ""
set cd_file = "$0"
set cd_file = $cd_file:r
set cd_file = "$cd_file".tar

switch ($machine)
  case 'iris'
    set nr_tape = "/dev/nrtape"
    breaksw;
  case 'sun4':
    set nr_tape = "/dev/nrst0"
    breaksw;
  case 'hp':
    set nr_tape = "/dev/rmt/0mn"    
    breaksw;
  case 'dec':
    set nr_tape = "/dev/nrmt0h"
    breaksw;
  default:
    set probl_no = 2
    goto problem
endsw

if ( -e $cd_file ) then
  set nr_tape = $cd_file
endif

# command line parsing
  set arguments = `getopt cnp:f: $*`
  if ( $status != 0 ) then
    echo "usage: tutor.ins [-f filename]"
    exit 2
  endif
  foreach i ( $arguments )
    switch ( $i ) 
      case '-c':
        set do_cd_home = 0 
        breaksw
      case '-n':
        set do_not_rewind = 1 
        breaksw
      case '-p':
        set position = $arguments[2]
        shift arguments
        breaksw
      case '-f':
        set nr_tape=$arguments[2]
        shift arguments
        breaksw
      case '--':
       break
    endsw
    shift arguments
  end


set MT = "mt -t $nr_tape"

if ( -f $nr_tape ) then # check wether nrtape is a normal file
  set do_not_rewind = 2 
endif

if ( $do_cd_home) then
  cd $HOME
endif

if ($do_not_rewind == 0) then
  $MT rewind
  $MT fsf $position
endif
if (($do_not_rewind == 1) && ($machine == dec)) then
  # skip tapemarker 
  $MT fsf 1
endif
#echo extracting tutorial from $nr_tape cwd: $cwd

tar xovf $nr_tape

if ($do_not_rewind == 0) then
  $MT rewind
endif

exit 0;

problem:
   echo problem nr $probl_no

exit $probl_no
#-----------------------------
